package com.jianglin.state2.controller;

import com.jianglin.state2.core.ResponsePack;

import com.jianglin.state2.dao.entity.Useer;
import com.jianglin.state2.model.dto.UseerFormDto;
import com.jianglin.state2.model.params.UseerAddParams;
import com.jianglin.state2.model.params.UseerUpdateParams;
import com.jianglin.state2.service.UseerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@ResponsePack
@RequestMapping("/useer")
@Api(tags = {"管理员"})
public class UseerController {


    private final UseerService useerService;

    @Autowired
    public UseerController(UseerService useerService) {
        this.useerService = useerService;
    }

    @GetMapping("/all")
    @ApiOperation("获得所有管理员")
    public List<Useer> getAll() {
        return useerService.list();
    }

    @GetMapping("/{userId}/{uesrpwd}")
    @ApiOperation("获取用户权限")
    public Integer getFrom(@ApiParam("用户ID") @PathVariable String userId,
                           @ApiParam("用户密码") @PathVariable String uesrpwd){
        Integer form = useerService.selectForm(userId, uesrpwd).get(0).getUserForm();
        if(form==0){
            return form;
        }
        if(form==1){
            return form;
        }
        if(form==2){
            return form;
        }
        if(form==3){
            return form;
        }
        if(form==4){
            return form;
        }
        return 10;
    }

    @PostMapping("/add")
    @ApiOperation("添加管理员")
    public boolean saveDesk(@Validated @RequestBody UseerAddParams useerAddParams) {
        Useer useer = useerAddParams.convertOut();
        return useerService.save(useer);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新管理员信息")
    public boolean updateEmployee(@ApiParam("管理员编号") @PathVariable String id,
                                  @Validated @RequestBody UseerUpdateParams useerUpdateParams){
        Useer useer = useerUpdateParams.convertOut();
        useer.setUserId(id);
        return useerService.updateById(useer);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除管理员")
    public boolean deleteUseer(@ApiParam("管理员编号") @PathVariable String id) {
        return useerService.removeById(id);
    }
}
